package com.passwordmanager.controller;

import com.passwordmanager.dto.auth.AuthResponse;
import com.passwordmanager.dto.auth.LoginRequest;
import com.passwordmanager.dto.auth.RegisterRequest;
import com.passwordmanager.service.AuthService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {

    private final AuthService authService;

    @PostMapping("/register")
    public ResponseEntity<AuthResponse> register(@Valid @RequestBody RegisterRequest request) {
        log.info("Received registration request for user: {}", request.getUsername());
        try {
            AuthResponse response = authService.register(request);
            log.info("Successfully registered user: {}", request.getUsername());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("Failed to register user: {}", request.getUsername(), e);
            throw e;
        }
    }

    @PostMapping("/login")
    public ResponseEntity<AuthResponse> login(@Valid @RequestBody LoginRequest request) {
        log.info("Received login request for user: {}", request.getUsername());
        try {
            AuthResponse response = authService.login(request);
            log.info("Successfully logged in user: {}", request.getUsername());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("Failed to login user: {}", request.getUsername(), e);
            throw e;
        }
    }

    @PostMapping("/mfa/verify")
    public ResponseEntity<AuthResponse> verifyMfaCode(
            @RequestParam String username,
            @RequestParam String mfaCode) {
        log.info("Received MFA verification request for user: {}", username);
        try {
            AuthResponse response = authService.verifyMfaCode(username, mfaCode);
            log.info("Successfully verified MFA for user: {}", username);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("Failed to verify MFA for user: {}", username, e);
            throw e;
        }
    }

    @GetMapping("/mfa/generate")
    public ResponseEntity<String> generateMfaQrCode(@RequestParam String username) {
        log.info("Generating MFA QR code for user: {}", username);
        try {
            String qrCode = authService.generateMfaQrCode(username);
            log.info("Successfully generated MFA QR code for user: {}", username);
            return ResponseEntity.ok(qrCode);
        } catch (Exception e) {
            log.error("Failed to generate MFA QR code for user: {}", username, e);
            throw e;
        }
    }

    @PostMapping("/mfa/enable")
    public ResponseEntity<Boolean> enableMfa(
            @RequestParam String username,
            @RequestParam String mfaCode) {
        log.info("Enabling MFA for user: {}", username);
        try {
            boolean result = authService.enableMfa(username, mfaCode);
            log.info("Successfully enabled MFA for user: {}", username);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("Failed to enable MFA for user: {}", username, e);
            throw e;
        }
    }

    @PostMapping("/mfa/disable")
    public ResponseEntity<Boolean> disableMfa(
            @RequestParam String username,
            @RequestParam String mfaCode) {
        log.info("Disabling MFA for user: {}", username);
        try {
            boolean result = authService.disableMfa(username, mfaCode);
            log.info("Successfully disabled MFA for user: {}", username);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("Failed to disable MFA for user: {}", username, e);
            throw e;
        }
    }
} 